package cn.mst.vrs.controller.admin.ordertype;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.mst.vrs.controller.admin.ordertype.vo.OrderTypeDictRespVO;
import cn.mst.vrs.controller.admin.ordertype.vo.OrderTypePageReqVO;
import cn.mst.vrs.controller.admin.ordertype.vo.OrderTypeRespVO;
import cn.mst.vrs.controller.admin.ordertype.vo.OrderTypeSaveReqVO;
import cn.mst.vrs.convert.OrderTypeConvert;
import cn.mst.vrs.dal.dataobject.ordertype.OrderTypeDO;
import cn.mst.vrs.service.ordertype.OrderTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 订单类型")
@RestController
@RequestMapping("/vrs/order-type")
@Validated
public class OrderTypeController {

    @Resource
    private OrderTypeService orderTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建订单类型")
    @PreAuthorize("@ss.hasPermission('vrs:order-type:create')")
    public CommonResult<Long> createOrderType(@Valid @RequestBody OrderTypeSaveReqVO createReqVO) {
        return success(orderTypeService.createOrderType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单类型")
    @PreAuthorize("@ss.hasPermission('vrs:order-type:update')")
    public CommonResult<Boolean> updateOrderType(@Valid @RequestBody OrderTypeSaveReqVO updateReqVO) {
        orderTypeService.updateOrderType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vrs:order-type:delete')")
    public CommonResult<Boolean> deleteOrderType(@RequestParam("id") Long id) {
        orderTypeService.deleteOrderType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vrs:order-type:query')")
    public CommonResult<OrderTypeRespVO> getOrderType(@RequestParam("id") Long id) {
        OrderTypeDO orderType = orderTypeService.getOrderType(id);
        return success(BeanUtils.toBean(orderType, OrderTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单类型分页")
    @PreAuthorize("@ss.hasPermission('vrs:order-type:query')")
    public CommonResult<PageResult<OrderTypeRespVO>> getOrderTypePage(@Valid OrderTypePageReqVO pageReqVO) {
        PageResult<OrderTypeDO> pageResult = orderTypeService.getOrderTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单类型 Excel")
    @PreAuthorize("@ss.hasPermission('vrs:order-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderTypeExcel(@Valid OrderTypePageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderTypeDO> list = orderTypeService.getOrderTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单类型.xls", "数据", OrderTypeRespVO.class,
                BeanUtils.toBean(list, OrderTypeRespVO.class));
    }

    @GetMapping("/list-dict")
    @Operation(summary = "字典列表")
    public CommonResult<List<OrderTypeDictRespVO>> listDict(){
        List<OrderTypeDO> orderTypeDOList = orderTypeService.listDict();
        return CommonResult.success(OrderTypeConvert.INSTANCE.toDictRespVOList(orderTypeDOList));
    }

}